package com.scrx.ymg.service.util;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.scrx.ymg.dao.direct.IDirectDao;
import com.scrx.ymg.dao.user.IUserDao;
import com.scrx.ymg.dao.util.IUtilDao;
import com.scrx.ymg.entity.Commission;
import com.scrx.ymg.entity.DirectInfo;
import com.scrx.ymg.entity.IndexBanner;
import com.scrx.ymg.util.BaseUtil;

@Service("utilService")
public class UtilServiceImpl implements IUtilService {

	@Autowired
	private IUtilDao utilDao;
	@Autowired
	private IUserDao userDao;

	@Autowired
	private IDirectDao directDao;

	public Commission getCommission() {

		return utilDao.getCommission();
	}

	public String updateCommission(Commission comm) {

		return utilDao.updateCommission(comm);
	}

	public String bindingMerchantID(int userID) {
		DirectInfo info = directDao.getDirectInfoById(1);
		info.setOpenId(userDao.findById(userID).getUserId());
		return directDao.updateDirectInfo(info);
	}

	public String deleteIndexBanner(int id) {
		return utilDao.deleteObject(utilDao.getObjectByParameter("IndexBanner", "id", id));
	}

	@Override
	public String updateIndexBanner(IndexBanner indexbanner, MultipartFile bannerFile) {
		String LicenseImgPath = "";
		IndexBanner oldbanner = (IndexBanner) utilDao.getObjectByParameter("IndexBanner", "id", indexbanner.getId());
		if (bannerFile.getSize() > 0) {
			String LicenseImg = BaseUtil.addRandom(bannerFile.getOriginalFilename());
			indexbanner.setBannerImg(LicenseImg);
			LicenseImgPath = BaseUtil.ProductPath + LicenseImg;
		} else {
			indexbanner.setBannerImg(oldbanner.getBannerImg());
		}
		return utilDao.updateIndexBanner(indexbanner, bannerFile, LicenseImgPath);
	}

	public List<IndexBanner> selectIndexBanner(IndexBanner indexbanner) {

		return utilDao.selectIndexBanner(indexbanner);
	}

	@Override
	public String createIndexBanner(IndexBanner indexbanner, MultipartFile bannerFile) {
		String directsPath = BaseUtil.ProductPath;
		String LicenseImg = BaseUtil.addRandom(bannerFile.getOriginalFilename());
		indexbanner.setBannerImg(LicenseImg);
		String LicenseImgPath = directsPath + LicenseImg;
		return utilDao.createIndexBanner(indexbanner, bannerFile, LicenseImgPath);
	}

}
